package com.padakeji.android.pworks.module.image.view;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import com.padakeji.android.pworks.R;
import com.padakeji.android.pworks.content.PadaImageLoaderFactory;
import com.padakeji.android.pworks.content.PadaImageLoader;
import com.padakeji.android.pworks.content.PadaResourceHelper;
import com.padakeji.android.pworks.content.UriProtocol;
import com.padakeji.android.pworks.module.image.common.ImageSettings;
import com.padakeji.android.pworks.module.image.entity.ImageDirectory;
import com.padakeji.android.pworks.module.image.entity.ImageInfo;
import com.padakeji.android.pworks.ui.UIHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AtanL on 2015/9/15.
 */
public class ImageGridAdapter extends RecyclerView.Adapter<ImageGridAdapter.ViewHolder> {

    public interface OnItemCheckListener {
        void onItemCheck(int directoryIndex, int positionInDirectory);
    }

    public interface OnCameraClickListener {
        void onCameraClick();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public ImageView checkboxImage;
        public ViewHolder(View v) {
            super(v);
            imageView = (ImageView) v.findViewById(R.id.pworks_activity_image_picker_image_item);
            checkboxImage = (ImageView) v.findViewById(R.id.pworks_activity_image_picker_image_check);
        }
    }

    private int maxSelectedCount = ImageSettings.MAX_SELECTED_COUNT;
    private OnItemCheckListener onItemCheckListener;
    private OnCameraClickListener onCameraClickListener;

    private int currentDirectoryIndex = 0;
    private Context mContext;
    private List<ImageDirectory> imageDirectories;

    private ArrayList<ImageInfo> selectedImages;

    private PadaImageLoader imageLoader;
    private PadaResourceHelper resourceHelper;

    public ImageGridAdapter(Context context, List<ImageDirectory> imageDirectories) {
        this(context, imageDirectories, ImageSettings.MAX_SELECTED_COUNT);
    }

    public ImageGridAdapter(Context context, List<ImageDirectory> imageDirectories, int maxSelectedCount) {
        if (context == null) {
            throw new RuntimeException("A context must be passed the constructor.");
        }
        if (imageDirectories == null) {
            imageDirectories = new ArrayList<>();
        }
        this.mContext = context;
        this.imageDirectories = imageDirectories;
        this.maxSelectedCount = maxSelectedCount;
        imageLoader = PadaImageLoaderFactory.getImageLoader(this.mContext);
        resourceHelper = new PadaResourceHelper(mContext);
        selectedImages = ImagePickerActivity.getSelectedImages();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.pworks_activity_image_picker_item_image, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final ImageDirectory currentImageDirectory = imageDirectories.get(currentDirectoryIndex);
        ImageInfo i = null;
        if (isCameraShown()) {
            if (position != 0) {
                i = currentImageDirectory.images.get(position - 1);
            }
        } else {
            i = currentImageDirectory.images.get(position);
        }
        final ImageInfo imageInfo = i;
        if (imageInfo != null) {
            String uri = UriProtocol.FILE + imageInfo.path;
            imageLoader.display(uri, holder.imageView);
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int currentItemIndex = isCameraShown() ? position - 1 : position;
                    previewImages(currentImageDirectory.images, currentItemIndex);
                }
            });
            holder.checkboxImage.setVisibility(View.VISIBLE);
            boolean isSelected = selectedImages.contains(imageInfo);
            holder.checkboxImage.setSelected(isSelected);
            holder.checkboxImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    toggleCheck(imageInfo);
                    notifyItemChanged(position);
                    onItemCheckListener.onItemCheck(currentDirectoryIndex, position);
                }
            });
        } else {
            holder.imageView.setImageResource(R.drawable.ic_photo_camera_black_24dp);
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onCameraClickListener.onCameraClick();
                }
            });
            holder.checkboxImage.setVisibility(View.GONE);
        }
    }

    private void previewImages(ArrayList<ImageInfo> images, int position) {
        Intent i = new Intent(mContext, ImagePreviewActivity.class);
        i.putExtra(ImagePreviewActivity.EXTRA_CURRENT_IMAGE_INDEX, position);
        i.putExtra(ImagePreviewActivity.EXTRA_IMAGE_INFOS, images);
        mContext.startActivity(i);
    }

    private void toggleCheck(ImageInfo imageInfo) {
        if (selectedImages.contains(imageInfo)) {
            selectedImages.remove(imageInfo);
        } else {
            if (isSelectEnable()) {
                selectedImages.add(imageInfo);
            } else {
                new UIHelper(mContext).showToast(resourceHelper.getString(R.string.pworks_image_tips_max_select, maxSelectedCount), Toast.LENGTH_SHORT);
            }
        }
    }

    public boolean isCameraShown() {
        return currentDirectoryIndex == 0;
    }

    public boolean isSelectEnable() {
        return selectedImages.size() < maxSelectedCount ? true : false;
    }

    @Override
    public int getItemCount() {
        if (imageDirectories.size() == 0) {
            return 0;
        }
        int count = imageDirectories.get(currentDirectoryIndex).images.size();
        if (isCameraShown()) {
            count += 1;
        }
        return count;
    }


    public void setCurrentDirectoryIndex(int position) {
        if (position < 0 || position > imageDirectories.size()) {
            return;
        }
        this.currentDirectoryIndex = position;
    }

    public ArrayList<ImageInfo> getSelectedImages() {
        return selectedImages;
    }

    public void setOnItemCheckListener(OnItemCheckListener onItemCheckListener) {
        this.onItemCheckListener = onItemCheckListener;
    }

    public void setOnCameraClickListener(OnCameraClickListener onCameraClickListener) {
        this.onCameraClickListener = onCameraClickListener;
    }

}
